package com.cq.hd.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cq.hd.admin.po.TbAdminUserPo;
import com.cq.hd.admin.po.TbBusinessAdminUserPo;
import com.cq.hd.admin.service.TbAdminUserService;
import com.cq.hd.admin.service.TbBusinessAdminUserService;
import com.cq.hd.common.enums.ResponseEnums;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Component
@Slf4j
public class BaseController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private TbAdminUserService adminUserService;

    @Autowired
    private TbBusinessAdminUserService businessAdminUserService;

    protected Integer getAdminUserId() {
        String userId = JwtUtil.getAudience(getToken(request), 0);
        if (StringUtils.isEmpty(userId)) {
            Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
        }

        return Integer.parseInt(userId);
    }

    protected Long getBusinessId() {
        String userId = JwtUtil.getAudience(getToken(request), 0);
        if (StringUtils.isEmpty(userId)) {
            Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
        }

        List<TbBusinessAdminUserPo> businessAdminUserPos = businessAdminUserService.list(new QueryWrapper<TbBusinessAdminUserPo>()
                .eq("delete_status", 0)
                .eq("admin_user_id", userId)
                .orderByDesc("create_time"));
        if (CollectionUtils.isEmpty(businessAdminUserPos)) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }

        return businessAdminUserPos.get(0).getBusinessId();
    }

    protected TbBusinessAdminUserPo getBusinessAdminUser() {
        String userId = JwtUtil.getAudience(getToken(request), 0);
        if (StringUtils.isEmpty(userId)) {
            Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
        }

        List<TbBusinessAdminUserPo> businessAdminUserPos = businessAdminUserService.list(new QueryWrapper<TbBusinessAdminUserPo>()
                .eq("delete_status", 0)
                .eq("admin_user_id", userId)
                .orderByDesc("create_time"));
        if (CollectionUtils.isEmpty(businessAdminUserPos)) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }

        return businessAdminUserPos.get(0);
    }

    protected String getToken(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (StringUtils.isEmpty(token)) {
            Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
        }

        return token;
    }

    protected TbAdminUserPo getCurrentUser() {
        try {
            Integer userId = getAdminUserId();
            if (userId == null) {
                Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
            }
            return adminUserService.getById(userId);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }
}
